package cn.mumzone.pos;

import android.content.Context;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.ArrayMap;

import com.alibaba.fastjson.JSON;
import com.sunmi.payment.PaymentService;
import com.tencent.bugly.crashreport.CrashReport;

import cn.jpush.android.api.JPushInterface;
import cn.mumzone.basic.BaseApplication;
import cn.mumzone.basic.util.SPManager;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.LoginEntity;
import cn.mumzone.pos.model.PopupHintEntity;
import cn.mumzone.pos.model.ShopConfigEntity;
import cn.mumzone.pos.model.source.AppRepository;
import cn.mumzone.pos.model.source.UserRepository;
import cn.mumzone.pos.util.PopupUtil;

/**
 * Created by qiuzhenhuan on 2019/6/11.
 */

public class MyApplication extends BaseApplication {


    @Override
    public void onCreate() {
        super.onCreate();

        //登录人信息
        UserRepository userRepository = new UserRepository();
        LoginEntity entity = userRepository.getLoginInfo();
        AppConstants.TEMP_TOKEN = entity.getToken();
        AppConstants.TEMP_COMPANY_ID = entity.getCompanyId();
        AppConstants.TEMP_SHOP_ID = entity.getShopId();
        AppConstants.TEMP_SELLER_ID = entity.getId();
        AppConstants.TEMP_SHOP_NAME = entity.getShopName();
        AppConstants.TEMP_USER_NAME = entity.getName();
        AppConstants.TEMP_BUSINESS_SCOPE = entity.getBusinessScope();
        AppConstants.TEMP_EQUI_MODEL = entity.getEquiModel();
        AppConstants.TEMP_MEMBER_LEVEL = entity.getLevelList();
        AppConstants.TEMP_SHOP_PHONE = entity.getShopPhone();
        AppConstants.TEMP_SHOP_ADDRESS = entity.getShopAddress();
        AppConstants.TEMP_USER_PERMISSION = entity.getPermission();
        AppConstants.TEMP_SHOP_STORE_TYPE = entity.getStoreType();
        AppConstants.TEMP_SHOP_PAY_CNL = entity.getPayCnl();
        AppConstants.TEMP_IS_INTEGRAL_DISCOUNT = entity.getIsIntegral();
        AppConstants.TEMP_IS_REDEEM_GOODS = entity.getIsRedeem();
        AppConstants.USER_POPUP_HINT = entity.getPopHint();
        AppConstants.ALL_HINT_DISMISSED = entity.isAllHintDismissed();
        if (!TextUtils.isEmpty(AppConstants.USER_POPUP_HINT)) {
            PopupUtil.hintEntity = JSON.parseObject(AppConstants.USER_POPUP_HINT, PopupHintEntity.class);
        }

        //设置信息
        AppRepository appRepository = new AppRepository();
        ShopConfigEntity config = appRepository.getAppConfig();
        AppConstants.CONFIG_OPEN_SCHEDULE = config.isOpenSchedule();
        AppConstants.CONFIG_PRINT_OFF_WORK_TICKET = config.isPrintOffWorkTicket();
        AppConstants.CONFIG_GUEST_DISPLAY = config.isShowGuestDisplay();
        AppConstants.CONFIG_PRINT_COUNT = config.getPrintCount();
        AppConstants.CONFIG_SHOW_SHOP_PHONE = config.isShowShopPhone();
        AppConstants.CONFIG_SHOW_SHOP_ADDRESS = config.isShowShopAddress();
        AppConstants.CONFIG_SHOW_BUY_NOTE = config.isShowBuyNote();
        AppConstants.CONFIG_SHOW_MEMBER_INFO = config.isShowMemberInfo();


        ArrayMap<String, String> arrayMap = SPManager.getInstance().getJson("", ArrayMap.class);
//        arrayMap.get

        //商米
        PaymentService.getInstance().init(this);

        //极光推送
        JPushInterface.setDebugMode(BuildConfig.DEBUG);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);
        JPushInterface.setAlias(this, 0, AppConstants.TEMP_SHOP_ID);//设置设备别名

        /**
         *
         * https://article.mumzone.cn/pos-beta2.0.apk
         https://article.mumzone.cn/pos-pre2.0.apk
         https://article.mumzone.cn/pos-sm-beta2.0.apk
         https://article.mumzone.cn/pos-sm-pre2.0.apk
         */

        String baseUrl;
        if (BuildConfig.DEBUG) { //测试
            baseUrl = "http://test3.mumzone.cn/";

            switch (BuildConfig.FLAVOR) {
                case "beta":
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-beta2.0.apk";
                    break;
                case "sunmibeta":
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-sm-beta2.0.apk";
                    break;
                case "pre":
                    baseUrl = "http://re.mumzone.cn/";
                    break;
                default:
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-beta2.0.apk";
                    break;
            }
        } else {
            switch (BuildConfig.FLAVOR) {
                case "pre": //预发布
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-pre2.0.apk";
                    baseUrl = "http://re.mumzone.cn/";
                    break;
                case "pro"://圣诞云正式
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-pro2.0.apk";
                    baseUrl = "http://service.mumzone.cn/";
                    break;
                case "sunmipre": //商米预发布
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-sm-pre2.0.apk";
                    baseUrl = "http://re.mumzone.cn/";
                    break;
                case "sunmipro"://商米正式
                    AppConstants.URL_APK_DOWMLOAD_PATH = AppConstants.URL_APK_DOWMLOAD_HOST + "pos-sm-pro2.0.apk";
                    baseUrl = "http://service.mumzone.cn/";
                    break;
                default:
                    baseUrl = "http://service.mumzone.cn/";
                    break;
            }
        }

        // 联网 BaseUrl
        SPManager mSp = SPManager.getInstance();
        String spUrl = mSp.getString("base_url", baseUrl);
        ApiRequest.init(spUrl);

        // Bugly 统计
        CrashReport.initCrashReport(getApplicationContext(),
                BuildConfig.DEBUG ? "be58cf7291" : "854301e947", BuildConfig.DEBUG);
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
